package com.template.project.controller;

import com.alibaba.fastjson2.JSONArray;
import com.template.project.constant.SwaggerTagConst;
import com.template.project.domain.PageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author xs
 * @date 2024/08/09 14:46
 */
@Api(tags = SwaggerTagConst.Support.CACHE)
@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController extends BaseController {
    @Resource
    private RestTemplate restTemplate;

    @ApiOperation(value = "测试1")
    @GetMapping("/test1")
    public String test1(@RequestParam("password") String password, @RequestParam("name") String name) {
        if (1 == 1) {
            throw new RuntimeException("error");
        }
        return "success";
    }

    @ApiOperation(value = "测试2")
    @PostMapping("/test2")
    public String test2(@RequestParam("file") MultipartFile file) {
        log.info(file.getName() + file.getOriginalFilename() + file.getSize());
        return "file success";
    }

    @ApiOperation(value = "测试3")
    @GetMapping("/test3")
    public String test3(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, @RequestParam("name") String name) {
        return "httpServletRequest success";
    }

    @GetMapping("/test4")
    public String test4() {
        System.out.println(getHttpServletRequest().getRequestURI());
        return "httpServletRequest success";
    }

    @GetMapping("/test5")
    public Object test5(@RequestParam String name) {
        String url = "https://query.aliyun.com/rest/aliyun-config.ace-services-module-list";
        UriComponentsBuilder.fromHttpUrl(url)
                .queryParam("timestamp", System.currentTimeMillis())
                .build()
                .toUri();
        // 返回响应体
        return restTemplate.getForObject(url, JSONArray.class);
    }

    @PostMapping("/test6")
    public String test6(@RequestParam String name, @RequestParam String pwd) {
        return "post success" + " name:" + name + " pwd:" + pwd;
    }

    @PostMapping("/test66")
    public String test66(@RequestParam Map<String, Object> map) {
        return "post success" + " name:" + map.get("name") + " pwd:" + map.get("pwd");
    }

    @PostMapping("/test666")
    public String test666(@RequestBody PageParam pageParam) {
        return pageParam.toString();
    }

    @PostMapping("/test7")
    public String test7() {
//        String url = "http://localhost:8848/demo/test6";
        String url = "http://localhost:8848/demo/test66";
        // 表单数据
        MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
        body.add("name", "myUsername");
        body.add("pwd", "myPassword");
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        // 构造请求实体
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(body, headers);
        // 发起POST请求
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                requestEntity,
                String.class
        );
        // 处理响应
        if (response.getStatusCode() == HttpStatus.OK) {
            return response.getBody();
        } else {
            throw new RuntimeException("Failed to submit form: " + response.getStatusCode());
        }
    }

    @PostMapping("/test8")
    public String test8() {
        String url = "http://localhost:8848/demo/test666";
        // 表单数据
        PageParam pageParam = new PageParam();
        pageParam.setPageNum(1);
        pageParam.setPageSize(10);
        pageParam.setSearchCount(Boolean.TRUE);
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        // 构造请求实体
        HttpEntity<PageParam> requestEntity = new HttpEntity<>(pageParam, headers);
        // 发起POST请求
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                requestEntity,
                String.class
        );
        // 处理响应
        if (response.getStatusCode() == HttpStatus.OK) {
            return response.getBody();
        } else {
            throw new RuntimeException("Failed to submit form: " + response.getStatusCode());
        }
    }

}
